package com.example.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;

import com.example.util.StringUtil;

public class SsoFilter implements Filter {
	public static final String IS_LOGIN ="is_login";
	public static final String SSO_LOGIN_STATUS_NOT ="2";//正在登陆，登陆尚未完成
	public static final String SSO_LOGIN_STATUS_SUCCESS ="1";//已经登陆
	public static final String SSO_LOGIN_URL ="";

	@Override
	public void destroy() {
		System.out.println("销毁过滤器");
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain filterChain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("进入sso-server过滤器");
		HttpServletRequest request=(HttpServletRequest) arg0;
		HttpServletResponse response=(HttpServletResponse) arg1;
		HttpSession session = request.getSession();
		String requestURI = request.getRequestURI();
		System.out.println(requestURI);
		//如果未登陆，跳转到SSO登陆页面
		if (!SSO_LOGIN_STATUS_SUCCESS.equals(session.getAttribute(IS_LOGIN))) {
			session.setAttribute(IS_LOGIN, SSO_LOGIN_STATUS_NOT);
			//分发到登陆页面
			request.getRequestDispatcher(SSO_LOGIN_URL).forward(request, response);
			return;
		}else {
			
		}
		//如果正在跳转，直接放行
		filterChain.doFilter(request, response);
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("初始化过滤器");
		
	}

}
